package com.itheima;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.dao.UserDao;
import com.itheima.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

@SpringBootTest
public class MpBatch {
    @Autowired
    private UserDao userDao;

    //批量操作
    @Test
    void test1(){
        List<Long> ids = new ArrayList<>();
        ids.add(8L);
        ids.add(7l);
        QueryWrapper<User> qw = new QueryWrapper<>();
        qw.in("id",ids);
        List<User> usersList = userDao.selectList(qw);
        System.out.println(usersList);
    }

    @Test
    void test2(){
        List<Long> ids = new ArrayList<>();
        ids.add(8L);
        ids.add(7l);
        //批量删除
        userDao.deleteBatchIds(ids);
    }

    //分组  按密码分统计数量
    @Test
    void test3(){
        QueryWrapper<User> qw = new QueryWrapper<>();
        //投影
        qw.select("password","count(*) as count");
        //分组
        qw.groupBy("password");

        List<Map<String, Object>> maps = userDao.selectMaps(qw);
        for (Map<String, Object> map : maps) {
            Set<Map.Entry<String, Object>> entries = map.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                Object value = entry.getValue();
                System.out.println("key= " + key +" "+"value= " + value);
            }
        }
    }
}
